<template>
  <div class="app-container">
    <el-form label-width="68px" size="small" :inline="true">
      <el-form-item label="菜单">
        <el-input placeholder="请输入菜单" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="small" @click="addHandle"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table :data="postList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="菜单名" align="center" prop="menuName" />
      <el-table-column label="顺序" align="center" prop="orderNum" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toView(scope.row)"
            >修改</el-button
          >
          <!-- 删除 -->
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form label-width="68px" ref="form" :model="form" :rules="rules">
        <el-form-item label="菜单名">
          <el-input
            placeholder="请输入菜单名"
            v-model="form.menuName"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="form.orderNum"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addWordMenu,
  getWordMenuList,
  eleWordMenu,
  getWordMenu,
  updateWordMenu,
} from "@/api/menuAPP/menu";
export default {
  data() {
    return {
      num: 1,
      title: "",
      total: 0,
      dialogFormVisible: false,
      postList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        menuName: "",
        orderNum: "",
      },
      rules: {
        menuName: [
          { required: true, message: "菜单名不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 修改按钮
    toView() {
      this.dialogFormVisible = true;
      this.title = "修改";
    },
    // 重置按钮
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
    },
    reset() {
      this.form = {
        menuName: "",
        orderNum: "",
      };
      this.resetForm("form");
    },
    addHandle() {
      this.reset();
      this.dialogFormVisible = true;
      title = "新增";
    },
    handelSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.menuId != null) {
            updateWordMenu(this.form).then((res) => {
              if (res.code == 200) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getList();
              }
            });
          } else {
            addWordMenu(this.form).then((res) => {
              if (res.code == 200) {
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    getList() {
      getWordMenuList(this.queryParams).then((res) => {
        this.postList = res.rows;
        this.total = res.total;
      });
    },
    handleQuery() {
      this.getList();
    },
    handleDelete(row) {
      eleWordMenu(row.menuId).then((res) => {
        if (res.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
    toView(row) {
      getWordMenu(row.menuId).then((res) => {
        this.reset();
        this.form = res.data;
        this.dialogFormVisible = true;
        this.title = "修改";
      });
    },
  },
};
</script>
<style scoped>
::v-deep .el-form-item--small .el-form-item__label {
  text-align: justify;
}
</style>
